package com.gcxy.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.conf.result.PageVO;
import com.gcxy.conf.result.R;
import com.gcxy.dto.RanchAddDTO;
import com.gcxy.dto.RanchPageDTO;
import com.gcxy.dto.RanchUpdateDTO;
import com.gcxy.entity.DataDicVal;
import com.gcxy.entity.Ranch;
import com.gcxy.mapper.RanchMapper;
import com.gcxy.service.ICityService;
import com.gcxy.service.IDataDicValService;
import com.gcxy.service.IRanchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcxy.service.IUserService;
import com.gcxy.vo.RanchVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 牧场管理 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-25
 */
@Service
public class RanchServiceImpl extends ServiceImpl<RanchMapper, Ranch> implements IRanchService {

    @Autowired
    private IUserService userService;
    @Autowired
    private IDataDicValService dataDicValService;
    @Autowired
    private ICityService cityService;

    @Override
    public R getRanchList(RanchPageDTO ranchPageDTO) {
        LambdaQueryWrapper<Ranch> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(ranchPageDTO.getRanchName()!=null,Ranch::getRanchName,ranchPageDTO.getRanchName())
                .eq(ranchPageDTO.getRanchStatus()!=null,Ranch::getRanchStatus,ranchPageDTO.getRanchStatus());
        Page<Ranch> ranchPage=new Page<>(ranchPageDTO.getPageNumber(),ranchPageDTO.getPageSize());
        ranchPage=page(ranchPage,queryWrapper);
        PageVO<RanchVO> ranchVOPageVO=new PageVO<>();
        BeanUtils.copyProperties(ranchPage,ranchVOPageVO);
        List<Ranch> ranchList=ranchPage.getRecords();
        List<RanchVO> ranchVOList=new ArrayList<>();
        for(Ranch r:ranchList){
            RanchVO ranchVO=new RanchVO();
            BeanUtils.copyProperties(r,ranchVO);
            DataDicVal dataDicVal=dataDicValService.getById(r.getValId());
            ranchVO.setValName(dataDicVal.getValName());
            ranchVOList.add(ranchVO);
        }
        ranchVOPageVO.setRecords(ranchVOList);
        return R.Success(ranchVOPageVO);
    }

    @Override
    public R deleteRanch(String id) {
        removeById(id);
        return R.Success("删除成功");
    }

    @Override
    public R updateRanch(RanchUpdateDTO ranchUpdateDTO) {
        Object userIdObj = StpUtil.getLoginId();
        String userId = (String) userIdObj;
        LambdaQueryWrapper<Ranch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Ranch::getRanchName, ranchUpdateDTO.getRanchName());
        Ranch ranch = getOne(queryWrapper);
        if (ranch!= null &&!ranch.getRanchId().equals(ranchUpdateDTO.getRanchId())) {
            return R.Failed("该牧场名称已存在");
        }
        DataDicVal dataDicVal=dataDicValService.getById(ranchUpdateDTO.getValId());
        if (dataDicVal.getValStatus()!=1){
            return R.Failed("该牧畜品种已被禁用");
        }
        Ranch ranch1=new Ranch();
        BeanUtils.copyProperties(ranchUpdateDTO, ranch1);
        ranch1.setProvince(cityService.getById(ranchUpdateDTO.getCityList().get(0)).getName());
        ranch1.setCity(cityService.getById(ranchUpdateDTO.getCityList().get(1)).getName());
        if (ranchUpdateDTO.getCityList().size() >= 3) {
            ranch1.setDistrict(cityService.getById(ranchUpdateDTO.getCityList().get(2)).getName());
        }
        LocalDateTime now = LocalDateTime.now();
        ranch1.setUpdater(userService.getById(userId).getUserName());
        ranch1.setUpdateTime(now);
        updateById(ranch1);
        return R.Success("修改成功！");
    }

    @Override
    public R getRanchById(String id) {
        return R.Success(this.getById(id));
    }

    @Override
    public R changeRanchStatus(String id) {
        Ranch ranch = getById(id);
        if (ranch == null) {
            return R.Failed("该农场不存在");
        }
        if (ranch.getRanchStatus() == 1) {
            if (ranch.getAmountLivestock()!=0){
                return R.Failed("有未出栏状态的牲畜,不能停用");
            }
            ranch.setRanchStatus(0);
        }else {
            ranch.setRanchStatus(1);
        }
        LocalDateTime now = LocalDateTime.now();
        Object userIdObj = StpUtil.getLoginId();
        String userId = (String) userIdObj;
        ranch.setUpdater(userService.getById(userId).getUserName());
        ranch.setUpdateTime(now);
        updateById(ranch);
        return R.Success("修改成功");
    }

    @Override
    public R addRanch(RanchAddDTO ranchAddDTO) {
        Object userIdObj = StpUtil.getLoginId();
        String userId = (String) userIdObj;
        LambdaQueryWrapper<Ranch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Ranch::getRanchName, ranchAddDTO.getRanchName());
        Ranch ranch = getOne(queryWrapper);
        if (ranch != null) {
            return R.Failed("该牧场名称已存在");
        }
        DataDicVal dataDicVal=dataDicValService.getById(ranchAddDTO.getValId());
        if (dataDicVal.getValStatus()!=1){
            return R.Failed("该牧畜品种已被禁用");
        }
        Ranch ranch1=new Ranch();
        BeanUtils.copyProperties(ranchAddDTO, ranch1);
        ranch1.setProvince(cityService.getById(ranchAddDTO.getCityList().get(0)).getName());
        ranch1.setCity(cityService.getById(ranchAddDTO.getCityList().get(1)).getName());
        if (ranchAddDTO.getCityList().size() >= 3) {
            ranch1.setDistrict(cityService.getById(ranchAddDTO.getCityList().get(2)).getName());
        }
        ranch1.setRanchStatus(1);
        LocalDateTime now = LocalDateTime.now();
        ranch1.setCreater(userService.getById(userId).getUserName());
        ranch1.setCreateTime(now);
        ranch1.setUpdater(userService.getById(userId).getUserName());
        ranch1.setUpdateTime(now);
        save(ranch1);
        return R.Success("新增成功！");
    }
}
